﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;

namespace BLL
{
    public class ThanhvienBLL : DAL.ThanhvienDAL
    {
        public string EncodeMD5(string chuoi)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);

            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);

            foreach (byte b in mang)
            {
                str_md5 += b.ToString("X2");
            }

            return str_md5;
        }

        public UTL.Session CheckLogin(string id, string pass)
        {
            //string mk = EncodeMD5(pass);
            string mk = pass;
            var usr = (DAL.Entities.Thanhvien)GetByKey(id);
            var sss = new UTL.Session();
            if (usr != null)
            {
                sss.Login = usr.matkhau == mk ? true : false;
                sss.Acc = usr.taikhoan;
                sss.Pass = usr.matkhau;
                sss.Name = usr.hoten;
                sss.Role = (UTL.Session.Roles)usr.vaitro;
            }
            else sss.Login = false;
            return sss;
        }

        public bool CheckPass(string _user, string pass2)
        {
            DataTable dtb = Select(_user);
            if (dtb != null)
            {
                string pass1 = dtb.Rows[0]["MATKHAU"] + "";
                if (pass1 == pass2)
                    return true;
            }
            return false;
        }
    }
}
